package coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"bp/internal/util"
	"context"
	"fmt"
)

// GetCouponPtr 获取优惠券
func (s *CouponEntityServiceImp) GetCouponPtr(ctx context.Context, q *sqlc.Queries, couponID int64) (couponPtr *sqlc.Coupon, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	coupon, err := q.GetCoupon(ctx, couponID)
	if err != nil {
		return
	}
	slog.Infof("1 GetCoupon ok")

	couponPtr = &coupon
	return
}

// ListCouponPtrsParam 列出优惠券参数
type ListCouponPtrsParam struct {
	CouponStatuses []string
	OfferTypes     []string
	Keyword        string
}

// ListCouponPtrs 列出优惠券
func (s *CouponEntityServiceImp) ListCouponPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponPtrsParam) (total int64, couponPtrs []*sqlc.Coupon, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCoupons(ctx, sqlc.CountCouponsParams{
		Ts:                    cPtr.PagerPtr.Ts,
		BrandID:               cPtr.BrandID,
		FilterAllCouponStatus: len(ptr.CouponStatuses) == 0,
		CouponStatuses:        ptr.CouponStatuses,
		FilterAllOfferType:    len(ptr.OfferTypes) == 0,
		OfferTypes:            ptr.OfferTypes,
		IsKeywordEmpty:        util.IsEmpty(ptr.Keyword),
		Keyword:               fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCoupons(ctx, sqlc.ListCouponsParams{
		Ts:                    cPtr.PagerPtr.Ts,
		BrandID:               cPtr.BrandID,
		FilterAllCouponStatus: len(ptr.CouponStatuses) == 0,
		CouponStatuses:        ptr.CouponStatuses,
		FilterAllOfferType:    len(ptr.OfferTypes) == 0,
		OfferTypes:            ptr.OfferTypes,
		IsKeywordEmpty:        util.IsEmpty(ptr.Keyword),
		Keyword:               fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:                cPtr.PagerPtr.Offset,
		Limit:                 cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListCouponPtrs ok")

	couponPtrs = s.MapCouponEntityToPtr(rows)
	return
}

// ListCouponPtrsByCouponIDs 根除多个优惠券id列出优惠券
func (s *CouponEntityServiceImp) ListCouponPtrsByCouponIDs(ctx context.Context, q *sqlc.Queries, couponIDs []int64) (couponPtrs []*sqlc.Coupon, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	rows, err := q.ListCouponsByCouponIDs(ctx, couponIDs)
	if err != nil {
		return
	}
	slog.Infof("1 ListCouponPtrsByCouponIDs ok")

	couponPtrs = s.MapCouponEntityToPtr(rows)
	return
}

// GetMapOfCouponIDToItemIDsByCouponIDs 获取优惠券ID到优惠券实体指针的映射
func (s *CouponEntityServiceImp) GetMapOfCouponIDToCouponPtrByCouponIDs(ctx context.Context, q *sqlc.Queries, couponIDs []int64) (m CouponIDToCouponPtrMap, err error) {
	m = make(CouponIDToCouponPtrMap)
	if len(couponIDs) == 0 {
		return
	}
	couponPtrs, err := s.ListCouponPtrsByCouponIDs(ctx, q, couponIDs)
	if err != nil {
		return
	}
	for _, couponPtr := range couponPtrs {
		m[couponPtr.ID] = couponPtr
	}
	return
}

// ListCouponWithStatPtrsParam 列出优惠券及其统计参数
type ListCouponWithStatPtrsParam struct {
	CouponStatuses []string
	OfferTypes     []string
	Keyword        string
}

// ListCouponWithStatPtrs 列出优惠券及其统计
func (s *CouponEntityServiceImp) ListCouponWithStatPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponWithStatPtrsParam) (total int64, couponWithStatPtrs []*sqlc.ListCouponWithStatsRow, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCoupons(ctx, sqlc.CountCouponsParams{
		Ts:                    cPtr.PagerPtr.Ts,
		BrandID:               cPtr.BrandID,
		FilterAllCouponStatus: len(ptr.CouponStatuses) == 0,
		CouponStatuses:        ptr.CouponStatuses,
		FilterAllOfferType:    len(ptr.OfferTypes) == 0,
		OfferTypes:            ptr.OfferTypes,
		IsKeywordEmpty:        util.IsEmpty(ptr.Keyword),
		Keyword:               fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCouponWithStats(ctx, sqlc.ListCouponWithStatsParams{
		Ts:                    cPtr.PagerPtr.Ts,
		BrandID:               cPtr.BrandID,
		FilterAllCouponStatus: len(ptr.CouponStatuses) == 0,
		CouponStatuses:        ptr.CouponStatuses,
		FilterAllOfferType:    len(ptr.OfferTypes) == 0,
		OfferTypes:            ptr.OfferTypes,
		IsKeywordEmpty:        util.IsEmpty(ptr.Keyword),
		Keyword:               fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:                cPtr.PagerPtr.Offset,
		Limit:                 cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListCouponWithStatPtrs (%d) ok", total)

	couponWithStatPtrs = s.MapCouponWithStatEntityToPtr(rows)
	return
}

// ListCouponPtrsParam 列出优惠券参数
type ListCouponWithDistributionStatPtrsParam struct {
	OwnerID         int64
	Issuer          string
	DistributeScene string
}

// ListCouponWithStatPtrs 列出优惠券及其投放统计(for C)
func (s *CouponEntityServiceImp) ListCouponWithDistributionStatPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponWithDistributionStatPtrsParam) (total int64, couponWithDistributionStatPtrs []*sqlc.ListCouponWithDistributionStatsByIssuerAndDistributeSceneRow, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCouponWithDistributionStatsByIssuerAndDistributeScene(ctx, sqlc.CountCouponWithDistributionStatsByIssuerAndDistributeSceneParams{
		Ts:      cPtr.PagerPtr.Ts,
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCouponWithDistributionStatsByIssuerAndDistributeScene(ctx, sqlc.ListCouponWithDistributionStatsByIssuerAndDistributeSceneParams{
		Ts:              cPtr.PagerPtr.Ts,
		BrandID:         cPtr.BrandID,
		MemberID:        cPtr.MemberID,
		OwnerID:         ptr.OwnerID,
		Issuer:          ptr.Issuer,
		DistributeScene: ptr.DistributeScene,
		Offset:          cPtr.PagerPtr.Offset,
		Limit:           cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}

	couponWithDistributionStatPtrs = s.MapCouponWithDistributionStatEntityToPtr(rows)
	return
}
